#include <iostream>
#include <iomanip>
#include <complex>

// 变量模板: 为每种类型定义Pi值
template<typename T>
constexpr T pi = T(3.1415926535897932385);

// 变量模板可以特化
template<>
constexpr const char* pi<const char*> = "3.14159";

// 带有非类型参数的变量模板
template<int N>
constexpr double power_of_two = 2.0 * power_of_two<N - 1>;

template<>
constexpr double power_of_two<0> = 1.0;

// 演示函数
void demonstrate_variable_templates() {
    // 使用不同类型的pi值
    std::cout << "  pi<int> = " << pi<int> << '\n';
    std::cout << "  pi<float> = " << pi<float> << '\n';
    std::cout << "  pi<double> = " << std::setprecision(15) << pi<double> << '\n';
    
    // 使用特化版本
    std::cout << "  pi<const char*> = " << pi<const char*> << '\n';
    
    // 在复杂类型中使用
    std::cout << "  pi<std::complex<double>> = " << pi<std::complex<double>> << '\n';
    
    // 使用带有非类型参数的模板
    std::cout << "\n  2的幂表:\n";
    std::cout << "  2^0 = " << power_of_two<0> << '\n';
    std::cout << "  2^1 = " << power_of_two<1> << '\n';
    std::cout << "  2^2 = " << power_of_two<2> << '\n';
    std::cout << "  2^3 = " << power_of_two<3> << '\n';
    std::cout << "  2^8 = " << power_of_two<8> << '\n';
} 